<?php
ob_start();
?>
<?php
include ('encabezado.php');
?>
<form action="edit.php" method="GET">
	<div>
	<?php
		//session_start();
		$user = $_SESSION['user'];
		$group = $_SESSION['group'];
		//$choosengroup = $_SESSION['choosen'];
		if(connectBD($ip, $userdb, $passworddb, $db))
		{
			if ($group == "Teacher")
			{
				$query = ("SELECT GroupID FROM Groups WHERE Name = '$choosengroup'");
				$idGroup = mysql_query($query);
				if($row = mysql_fetch_assoc($idGroup))
				{
					$idGroup = $row['GroupID'];
				}
				$query = ("SELECT UserID, Name, Surname FROM Users WHERE GroupID = '$idGroup'");
				$idUser = mysql_query($query);
				echo "<br><br><h2 align='center'>View Marks</h2><table class='marks' border='2' align='center'>";
				echo "<tr><td>Name</td><td>UserID</td><td>Mark</td><td>Type</td><td>Description</td><td>Date</td></tr>";
				while ($row = mysql_fetch_assoc($idUser))
				{
					$id = $row['UserID'];
					$username = $row['Name'];
					$query = ("SELECT * FROM Marks WHERE UserID = '$id'");
					$id = mysql_query($query);					
					
					while ($row = mysql_fetch_assoc($id))
					{
						echo "<tr><td>".$username."</td><td>".$row['UserID']."</td><td>".$row['Mark']."</td><td>".$row['Type']."</td>
						<td>".$row['Description']."</td><td>".$row['Date']."</td><td border='none'><a href='edit.php?edit={$row['MarkID']}'/>Edit</a></td>
						<td border='none'><a href='delete.php?delete={$row['MarkID']}'/>Delete</a></td></tr>";
					}
					
				}
				echo "</table>";
			}	
			else
			{
			$query = ("SELECT UserID FROM Users WHERE Name = '$user'");
			$idUser = mysql_query($query);
			$id = mysql_fetch_assoc($idUser);
			$id = $id['UserID'];
			$query = ("SELECT * FROM Marks where UserID = '$id'");
			$result = mysql_query($query);
			
			echo "<br><br><h2 align='center'>View Marks</h2><table class='marks' border='2' align='center'>";
			echo "<tr><td>Mark</td><td>Type</td><td>Description</td><td>Date</td></tr>";
			while($row = mysql_fetch_assoc($result))
			{
				echo "<tr><td>".$row['Mark']."</td><td>".$row['Type']."</td><td>".$row['Description']."</td><td>".$row['Date']."</td></tr>";
			}
			echo "</table>";
			}
		}
		else
		{
			echo "Conex error";
		}
	?>
	</div>
</form>
<?php
include ('pie.php');
?>
<?php
ob_end_flush();
?>